import { hasToken } from '@/utils/auth'
import { Route, Redirect, RouteProps } from 'react-router-dom'

function PrivateRoute({ children, ...rest }: RouteProps) {
  return (
    // 合并其他的属性 到Route上
    <Route
      {...rest}
      render={({ location }) => {
        console.log('location:', location)
        return hasToken() ? (
          children
        ) : (
          <Redirect
            to={{
              pathname: '/login',
              state: { from: location.pathname },
            }}
          />
        )
      }}
    />
  )
}

export default PrivateRoute
